commitment = {}


commitment.list = function(){
    main.init();
    fly.ajax({
        service: '/commitment/list',
        success: function(result){
            if(result.status){
                $("#content").html(fly.template('http://naima.vn/commitment/list.html',{
                    data: result.data
                }));
                commitment.load();
                commitment.printSeller();
                $('#parentId').change(function(){
                    commitment.load();
                });
            } else {
                popup.msg(result.message);
            }
        }
    });
    
}
commitment.seller;
commitment.getSellerName = function(sellerId){
    var name = commitment.getSeller();
    var sellername;
    $.each(name, function(){
        var tp = this;
        if(tp.id == sellerId){
            sellername = tp.name;
        }
    })
    return sellername;
}
commitment.getSeller = function(){
    if(!commitment.seller){
        fly.ajax({
            service: '/seller/list',
            loading: false,
            async: false,
            success: function(rs){                
                commitment.seller = rs.data;
            }
        });
    }
    return commitment.seller;
}
commitment.printSeller = function(){
    var seller = commitment.getSeller();
    $('.seller').html('<option value="0">Tất cả</option>');
    $.each(seller, function(){
        var tmp = this;
        $('.seller').append('<option value='+tmp.id+'>'+tmp.name+'</option>');
    });                
}
commitment.load = function(){
    fly.ajax({
        service: '/commitment/list',
        data: {
            sellerId: $("#parentId").val() 
        },
        async: false,
        success: function(result){
            if(result.status){
                //Hiển thị danh sách
                $("#tbl-list").html(fly.template('http://naima.vn/commitment/sub.html',{
                    data:result.data
                }));                             
            } else {
                popup.msg(result.message);
            }
        }
    });
    
}

commitment.create = function(){
    popup.open('popup-commitment-form', 'Thêm bài viết mới', fly.template('http://naima.vn/commitment/form.html',{
        data: null
    }),[
    {
        title: 'Thêm mới',
        fn:function(){
            fly.submit({
                id: 'commitment-form',
                service: '/commitment/create',
                success: function(result){
                    popup.msg(result.message);
                    commitment.load();
                    popup.close('popup-commitment-form');
                }
            });
        }
    },
    {
        title: 'Hủy bỏ',
        fn:function(){
            popup.close('popup-commitment-form');
        }
    },
    ]);
    commitment.printSeller();
    fly.editor('txt-content',{
        width:800
    });  
}

commitment.edit = function(id){
    fly.ajax({
        service: '/commitment/get/',
        data: {
            id: id
        },
        success: function(result){
            popup.open('popup-commitment-form', 'Sửa bài viết', fly.template('http://naima.vn/commitment/form.html',{
                data:result.data
            }),[
            {
                title: 'Sửa',
                fn:function(){
                    fly.submit({
                        id: 'commitment-form',
                        service: '/commitment/edit',
                        success: function(rs){
                            popup.msg(rs.message);
                            commitment.load();
                            popup.close('popup-commitment-form');
                        }
                    });
                }
            },
            {
                title: 'Hủy bỏ',
                fn:function(){
                    popup.close('popup-commitment-form');
                }
            }
            ]);
            commitment.printSeller();
            fly.editor('txt-content',{
                width:800
            });  
            $('#commitment-form .seller').val(result.data.sellerId);
        }
        
    });
     
}

commitment.del = function(id){
    popup.confirm("Bạn có chắc muốn xóa bài viết này ?", function(){
        fly.ajax({
            service: '/commitment/delete',
            data: {
                id:id
            },
            success: function(result){
                popup.msg(result.message);
                $(".table tr[for="+result.data.id+"]").remove();
            }
        });
    });
    
}


commitment.stripHTML = function(oldString) {

    var newString = "";
    var inTag = false;
    for(var i = 0; i < oldString.length; i++) {
   
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
            if(oldString.charAt(i+1)=="<")
            {
            //dont do anything
            }
            else
            {
                inTag = false;
                i++;
            }
        }
   
        if(!inTag) newString += oldString.charAt(i);

    }

    return newString;

}